package com.woniuxy.user.controller;

import com.woniuxy.commons.entity.*;
import com.woniuxy.commons.service.RemoteCourseService;
import com.woniuxy.user.service.UserCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:阳佳
 * @Date: 2020/7/20 17:38
 * @Description:
 * @PACKAGE_NAME:com.woniuxy.user.controller
 */
@Api(tags = "收藏相关操作")
@RestController
@RequestMapping("/user")
public class UserCollectController {
    @Autowired
    private UserCollectService userCollectService;
    @Autowired
    private RemoteCourseService remoteCourseService;

    @ApiOperation("对某个课程进行收藏")
    @PostMapping("collect/{cid}")
    @ResponseBody
    public ResponseEntity collect(@ApiParam("课程id") @PathVariable String cid) {
        //进入某个课程后 可以对某个课程进行收藏
        // User user = (User) httpSession.getAttribute("user");
        //获取用户id
        String uid = "1";//user.getUid();
        //判断用户是否对改课件进行了收藏
        if (userCollectService.findCollectRecordByUidAndCid(uid, cid) == null) {
            UserCollect userCollect = new UserCollect().setUid(uid).setCid(cid);
            //新增一条收藏记录
            userCollectService.addCollectRecord(userCollect);
            return new ResponseEntity(004, "收藏成功");
        }
        //取消收藏记录记录
        userCollectService.cancelCollect(uid, cid);
        return new ResponseEntity<>(005, "取消收藏");
    }

    /*
    查看某个课程是否被用户收藏
     */
    @ApiOperation("判断某个课程是否被收藏")
    @GetMapping("isCollect/{cid}")
    public ResponseEntity isCollect(HttpSession httpSession, @ApiParam("课程id") @PathVariable String cid) {
        User user = (User) httpSession.getAttribute("user");
        //获取用户id
        String uid = "1";//user.getUid();
        if (userCollectService.findCollectRecordByUidAndCid(uid, cid) == null) {
            return new ResponseEntity<>(-1, "未被收藏");
        }
        return new ResponseEntity<>(1, "已收藏");
    }

    /*
    在“我的收藏”页面进行取消收藏操作
     */
    @ApiOperation("根据课程id 用户id取消收藏")
    @DeleteMapping("cancelCollect/{cid}")
    public ResponseEntity cancelCollect(HttpSession httpSession, @ApiParam("课程id") @PathVariable String cid) {
        User user = (User) httpSession.getAttribute("user");
        //获取用户id
        String uid = "1";//user.getUid();
        //取消收藏记录记录
        userCollectService.cancelCollect(uid, cid);
        return new ResponseEntity<>(6, "取消收藏");
    }

    /*
    查看我的全部收藏
     */
    @ApiOperation("查询用户所有收藏")
    @GetMapping("myAllCollect")
    @ResponseBody
    public ResponseEntity myAllCollect(HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        //获取用户id
        String uid = "1";//user.getUid();
        //得到查询结果
        List<UserCollect> list = userCollectService.myAllCollect(uid);
        //此list用来存储查询出来的课程
        List<CollectionVo> listForCollections=new ArrayList<>();
        //得到cwid再通过调用其他微服务远程接口 查出课件详情
        for (int i = 0; i < list.size(); i++) {
            //得到cid
            String cid = list.get(i).getCid();
            // 调用微服务接口  将查询到的值赋值给CollectionVo
            CollectionVo collectionVo = new CollectionVo();
            Course course = remoteCourseService.findCourseByCid(Integer.parseInt(cid)).getData();
            collectionVo.setCname(course.getCname());//课程名字
            collectionVo.setCover(course.getCover());//课程封面
            collectionVo.setCoursewareNum(course.getCoursewareNum());//该课程包含多少个课件
            collectionVo.setCollectTime(new Date());
            collectionVo.setCid(cid);
            listForCollections.add(collectionVo);
        }
        return new ResponseEntity(007, "查看全部收藏", listForCollections);
    }
}
